class SchoolInfoController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

//    def index = {
//        redirect(action: "list", params: params)
//    }

//    def list = {
//        params.max = Math.min(params.max ? params.int('max') : 10, 100)
//        [schoolInfoInstanceList: SchoolInfo.list(params), schoolInfoInstanceTotal: SchoolInfo.count()]
//    }

//    def create = {
//        def schoolInfoInstance = new SchoolInfo()
//        schoolInfoInstance.properties = params
//        return [schoolInfoInstance: schoolInfoInstance]
//    }
//
//    def save = {
//        def schoolInfoInstance = new SchoolInfo(params)
//
//        log.info "Save schoolInfo logo id : ${schoolInfoInstance?.logo?.id} "
//        def school = School.get(session.schoolID)
//        schoolInfoInstance.school  = school
//
//        if (schoolInfoInstance.save(flush: true)) {
////            def school = session.school
//            school.schoolInfo = schoolInfoInstance
//            school.save()
//            flash.message = "${message(code: 'default.created.message', args: [message(code: 'schoolInfo.label', default: 'SchoolInfo'), schoolInfoInstance])}"
//            redirect(action: "list")
//        }
//        else {
//            render(view: "create", model: [schoolInfoInstance: schoolInfoInstance])
//        }
//    }

//    def show = {
//        def schoolInfoInstance = SchoolInfo.get(params.id)
//        if (!schoolInfoInstance) {
//            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'schoolInfo.label', default: 'SchoolInfo'), params.id])}"
//            redirect(action: "list")
//        }
//        else {
//            [schoolInfoInstance: schoolInfoInstance]
//        }
//    }

    def edit = {
        def school = School.get(session?.school?.id)
        log.info "schoolInfo.edit ${session?.school?.id}"
        def schoolInfoInstance 
        if(school.schoolInfo){
            schoolInfoInstance = school.schoolInfo
        }else{
            schoolInfoInstance = new SchoolInfo(name:school.name,school:school)
            schoolInfoInstance.save()
            school.schoolInfo = schoolInfoInstance
            school.save()
        }
        
        if (!schoolInfoInstance) {
            flash.message = "没有找到这个学校的信息。" 
        } 
        return [schoolInfoInstance: schoolInfoInstance]
       
    }

    def update = {
        def schoolInfoInstance = SchoolInfo.get(params.id)
        if (schoolInfoInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (schoolInfoInstance.version > version) {
                    
                    schoolInfoInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'schoolInfo.label', default: 'SchoolInfo')] as Object[], "Another user has updated this SchoolInfo while you were editing")
                    render(view: "edit", model: [schoolInfoInstance: schoolInfoInstance])
                    return
                }
            }
            schoolInfoInstance.properties = params
            if (!schoolInfoInstance.hasErrors() && schoolInfoInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'schoolInfo.label', default: 'SchoolInfo'), schoolInfoInstance])}"
                redirect(action: "edit")
            }
            else {
                render(view: "edit", model: [schoolInfoInstance: schoolInfoInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'schoolInfo.label', default: 'SchoolInfo'), params.id])}"
            redirect(action: "edit")
        }
    }

    def delete = {
//        def schoolInfoInstance = SchoolInfo.get(params.id)
//        if (schoolInfoInstance) {
//            try {
//                schoolInfoInstance.delete(flush: true)
//                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'schoolInfo.label', default: 'SchoolInfo'), params.id])}"
//                redirect(action: "list")
//            }
//            catch (org.springframework.dao.DataIntegrityViolationException e) {
//                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'schoolInfo.label', default: 'SchoolInfo'), params.id])}"
//                redirect(action: "show", id: params.id)
//            }
//        }
//        else {
//            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'schoolInfo.label', default: 'SchoolInfo'), params.id])}"
//            redirect(action: "list")
//        }
    }
}
